package com.example.springboot.controller;

import com.example.springboot.entity.JsonResult;
import com.example.springboot.entity.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }

    @RequestMapping("hello2")
    public ArrayList<String> hello2() {
        //创建对象 jdk基本类型
        ArrayList<String> sites = new ArrayList<String>();
        //添加
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");


//        System.out.println(sites);
//        //遍历
//        for (int i=0; i< sites.size();i++){
//            String s = sites.get(i);
//            System.out.println(s);
//        }
        return sites;
    }

    @RequestMapping("hello3")
    public List<User> hello3() {
        //创建对象 引用类型
        List<User> sites = new ArrayList<>();
        User user = new User();
        user.setUsername("zhangsan");
        user.setPassword("123456");
        user.setSex("男");
        user.setBirthday(new Date());
        sites.add(user);

        //创建
        User user2 = new User();
        user2.setUsername("李四");
        user2.setPassword("123456");
        user2.setSex("女");
        Date birthday = new Date();
        System.out.println(birthday);
        user2.setBirthday(birthday);
        sites.add(user2);

        User user1 = new User("wangwu", "123456", "男", new Date());
        sites.add(user1);

        Date date = new Date(System.currentTimeMillis());
        System.out.println(user);
        System.out.println(date);


        System.out.println("==================");
        System.out.println(sites);
        return sites;
    }

    @RequestMapping("hello4")
    public User hello4() {
        User user = new User("wangwu", "123456", "男", new Date());
        return user;
    }

    @RequestMapping("hello5")
    public Map<String, Object> hello5() {
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", "zhangsan");
        hashMap.put("age", 15);

        String o = (String) hashMap.get("1");

        HashMap<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "123");
        String s = map.get(1);

        HashMap<String, Integer> map2 = new HashMap<>();
        map2.put("123", 1);
        System.out.println("=================");
        System.out.println(hashMap);
        return hashMap;
    }

    @RequestMapping("hello6")
    public JsonResult<Map> hello6() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "zhangsan");
        JsonResult<Map> result = new JsonResult<>(map, "200", "操作成功");
        return result;
    }

    @RequestMapping("hello7")
    public JsonResult<Map> hello7() {
        return new JsonResult<>(null, "500", "服务器异常");
    }


    @RequestMapping("hello8")
    public JsonResult<ArrayList> hello8() {
        //创建对象 jdk基本类型
        ArrayList<String> sites = new ArrayList<String>();
        //添加
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");

        JsonResult<ArrayList> arrayListJsonResult = new JsonResult<ArrayList>();
//        arrayListJsonResult.setCode("200");
//        arrayListJsonResult.setMsg("操作成功");
//        arrayListJsonResult.setData(sites);
        String username = "";
        arrayListJsonResult.setCode("500");
        arrayListJsonResult.setMsg("用户名无效");
        return arrayListJsonResult;
    }

    /*
    * 查询用户，如果用*/
    @RequestMapping("hello9")
    public JsonResult<User> hello9(@RequestParam("username") String username) {
        JsonResult<User> arrayListJsonResult = new JsonResult<>();
        //判断两个值是否相等
        if ("".equals(username)){
            arrayListJsonResult.setCode("500");
            arrayListJsonResult.setMsg("用户名无效");
        }else {
            User user = new User();
            user.setUsername(username);
            user.setPassword("123456");
            user.setSex("女");
            arrayListJsonResult.setData(user);
            arrayListJsonResult.setMsg("操作成功");
            arrayListJsonResult.setCode("200");
        }
        return arrayListJsonResult;
    }

}
